﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace BidPreparation.model
{
    public sealed class MysqlConnectionHelper
    {
        private MySqlConnection conn = null;

        private static MysqlConnectionHelper instance = null;
        private static readonly object padlock = new object();

        private MysqlConnectionHelper() { }

        public static MysqlConnectionHelper Instance {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    { 
                        instance = new MysqlConnectionHelper(); 
                    }
                }
                return instance;
            } 
        }


        public MySqlConnection Conn
        {
            get { 
                if (conn == null)
                {
                    MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
                    //数据库连接时的用户名，可以用pid
                    builder.UserID = "xxx";
                    //数据库连接时的密码，可以用pwd
                    builder.Password = "xxx";
                    //数据库连接时的服务器地址
                    builder.Server = "localhost";
                    //要连接的数据库
                    builder.Database = "xxx";

                    //定义与数据连接的链接
                    conn = new MySqlConnection(builder.ConnectionString);
                    //打开这个链接
                    try
                    {
                        conn.Open();

                        return conn;
                    }
                    catch
                    {
                        Console.WriteLine("open exception");
                    }
                }
                else
                {
                    return conn;
                }

                return null;
            }
        }
    }
}
